package com.jt.shop.service.impl;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.jt.shop.mapper.UserMapper;
import com.jt.shop.model.ShopException;
import com.jt.shop.model.User;
import com.jt.shop.service.UserService;

/**
 * Created by he on 2015/9/5.
 */
@Service("userService")
public class UserServiceImpl implements UserService {


    private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);

    @Resource
    private UserMapper userMapper;

    public int add(User user) {
        String password = DigestUtils.md2Hex(user.getPassword());
        user.setPassword(password);
        return userMapper.add(user);
    }

    public void update(User user) {
        userMapper.upate(user);
    }

    public void delete(int id) {
        userMapper.delete(id);
    }

    public User find(int id) {
        return userMapper.find(id);
    }

    public User findByEmail(String email) {
        return userMapper.findByEmail(email);
    }

    public User login(String username, String password) {
        User us = userMapper.login(username);
        log.info("登陆用户: {}", us);
        if (us == null || !us.getPassword().equals(password)) {
            throw new ShopException("用户名或密码错误");
        }
        return us;
    }
}
